import defaultSettings from '@/settings'
import { getTheme, setTheme, removeTheme } from '@/utils/auth'

const { showSettings, fixedHeader, sidebarLogo, theme } = defaultSettings

const state = {
  showSettings: showSettings,
  fixedHeader: fixedHeader,
  sidebarLogo: sidebarLogo,
  theme: getTheme() || theme
  // theme: '#1890FF'
}

const mutations = {
  CHANGE_SETTING: (state, { key, value }) => {
    // eslint-disable-next-line no-prototype-builtins
    if (state.hasOwnProperty(key)) {
      state[key] = value
      // 这里可以通过传不同的参，改不同的值，所以加判断，如果要改的是theme，做本地存储
      if (key === 'theme') {
        setTheme(value)
      }
    }
  },
  resetTheme(state) {
    removeTheme()
    state.theme = theme
  }
}

const actions = {
  changeSetting({ commit }, data) {
    commit('CHANGE_SETTING', data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
